(function($) {
	var treegrid = '#dg_menu', toolbar = '#toolbars_menu', add = '#add_menu', del = '#delete_menu', update = '#update_menu', reload = '#reload_menu',
		form = '#form_menu', submit = '#submit', closeWin = '#closeWin', search = '#search', combotree_menu = '#combotree_menu',
		cleanCache = '#cleanCache';
	
	
	var menuObj = function() {
		var self = this ;
		this.tg = null ;
		this.url_form = window.app.domain.URL_ADMIN_UI + '/pages/menu/form.html' ;
		this.url_list = window.app.domain.URL_API_MANAGER_REST + '/menu/treegrid' ;
		this.url_add = window.app.domain.URL_API_MANAGER_REST + '/menu/add' ;
		this.url_update = window.app.domain.URL_API_MANAGER_REST + '/menu/update' ;
		this.url_delete = window.app.domain.URL_API_MANAGER_REST + '/menu/delete' ;
		this.url_get = window.app.domain.URL_API_MANAGER_REST + '/menu/get' ;
		this.url_cleanCache = window.app.domain.URL_API_MANAGER_REST + '/menu/clean' ;
		
		this.inittreegrid() ;
		this.initBindBtnEvent();
		
	};
	
	menuObj.prototype.inittreegrid = function() {
		var self = this ;
		this.tg = $(treegrid).treegrid({
			url: self.url_list, 
			method: 'GET', nowrap: true, striped: true, singleSelect: false,
			idField: "id", treeField: "text", rownumbers: true, cascadeCheck: true, border: false,
			toolbar: "#toolbars_menu", striped: true, fit: true, remoteSort: false, singleSelect: true,
			frozenColumns: [[
			           {title: 'ID', field: 'id', hidden: true},
			           {field: 'ck', checkbox:true}
			]],
			columns: [[
			           {title: '排序', field: 'orders', width: 40, align: 'right'},
			           {title: '菜单名称', field: 'text', width: 200},
			           {title: '页面URL', field: 'href', width: 400},
			           {title: '唯一标识', field: 'code', width: 150},
			           {title: '权限表示', field: 'permission', width: 150},
			           {title: '打开方式', field: 'iniframe', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">Ajax</span>';
			        	   else return '<span class="label label-danger">IFrame</span>';
			           }},
			           {title: '状态', field: 'disabled', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">正常</span>';
			        	   else return '<span class="label label-danger">禁用</span>';
			           }},
			           {title: '选中', field: 'selected', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">选中</span>';
			        	   else return '<span class="label label-danger">不选中</span>';
			           }},
			           {title: '展开', field: 'opened', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">展开</span>';
			        	   else return '<span class="label label-danger">不展开</span>'; 
			           }},
			           {title: '图标', field: 'icons', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return '<span class="'+value+'" style="color: '+row.color+'"></span>';
			           }},
			           {title: '颜色', field: 'color', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return '<i style="color:'+value+'">'+value+'</i>';
			           }},
			           {title: '创建时间', field: 'createTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			           {title: '更新时间', field: 'updateTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			]],
			onLoadSuccess: function(data) {
			}
		});
	};
	
	menuObj.prototype.initBindBtnEvent = function() {
		var self = this ;
		$(add).on('click', function() { self.add() });
		$(del).on('click', function() { self.del() });
		$(update).on('click', function() { self.update() });
		$(reload).on('click', function() { self.reload() });
		$(cleanCache).on('click', function() { self.cleanCache(); });
	};
	
	menuObj.prototype.add = function() {
		var self = this ;
		window.app.easyui.addWindow({title: '添加', href: self.url_form, width: 650, height: 600,
			onLoad: function() {
				var selections = window.app.easyui.getSelections(treegrid) ;
				var cbt_menu = $(combotree_menu).combotree({url : self.url_list, method: 'get'});
				if(undefined != selections) {
					cbt_menu.combotree('setValue', selections[0]) ;
				}
				$(submit).on('click', function() {self.submitForm(self.url_add);});
			}
		});
	};
	
	menuObj.prototype.update = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(treegrid) ;
		if(selections) {
			if(selections.length == 1) {
				window.app.easyui.addWindow({title: '修改', href: self.url_form, width: 650, height: 600,
					onLoad: function() {
						var parentId ;
						$.get(self.url_get + '/' + selections[0], function(result) {
							$(form).form('load', result);
							parentId = result.parent ;
						});
						$(combotree_menu).combotree({
							url : self.url_list, method: 'get',
							onChange: function(newValue, oldValue){
								//当前ID不能作为父节点
								if(newValue == selections[0]) {
									$(combotree_menu).combotree('setValue', parentId) ;
									$.messager.alert('Warning','当前ID不能作为父节点');
								}
					        }
						});
						
						$(submit).on('click', function() {self.submitForm(self.url_update);});
					}
				});
			} else {
				alertify.error('只能选择一条记录！');
			}
		} else {
			alertify.error('请选择记录！');
		}
		
	};
	
	menuObj.prototype.del = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(treegrid) ;
		if(selections) {
			$.messager.confirm('删除确认', '确定要删除？', function(r) {
				if(r) {
					$.ajax({
						type: 'GET',
						url: self.url_delete + '/' + selections.join(','),
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	menuObj.prototype.get = function(id) {
		var self = this ;
		$.ajax({
			type: 'GET',
			url: self.url_get + id,
			beforeSend: function() {
				$.easyui.loading();
			},
			success: function(result) {
				try {
					return result ;
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			},
			complete: function(xhr, textStatus) {
				$.easyui.loaded();
			}
		});
	};
	
	menuObj.prototype.submitForm = function(url) {
		var self = this ;
		var checkform = $(form).form('validate');
		if(checkform) {
			$.ajax({
				type: 'POST',
				url: url,
				data: window.app.util.getData(form),
				beforeSend: function() {
					$.easyui.loading();
				},
				success: function(result) {
					try {
						if(result.status) {
							self.reload(); window.app.easyui.closeCurrentWindow(); alertify.success(result.message);
						} else {
							alertify.warning(result.message);
						}
					} catch(e) {
						$.easyui.loaded(); console.error(e) ;
					}
				},
				complete: function(xhr, textStatus) {
					$.easyui.loaded();
				}
			});
		}
	};
	
	menuObj.prototype.reload = function() {
		this.tg.treegrid('reload'); this.tg.treegrid('clearSelections');this.tg.treegrid('clearChecked');
	};
	
	menuObj.prototype.cleanCache = function() {
		var self = this ;
		$.get(self.url_cleanCache, function(result) {
			self.tg.treegrid('reload');
			alertify.success(result.message);
		});
 	};
	
	var menu = new menuObj() ;
})(jQuery);
	
